package cn.kinoko.controller.export;

import cn.kinoko.common.aspect.annotation.Lock;
import cn.kinoko.common.base.model.BaseController;
import cn.kinoko.common.base.model.Result;
import cn.kinoko.common.constant.CacheKeyConstant;
import cn.kinoko.service.common.export.component.ExportManager;
import cn.kinoko.service.common.export.component.ExportQueue;
import cn.kinoko.service.common.util.AuthUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author kk
 */
@RequestMapping("/export")
@RestController
public class ExportTaskController extends BaseController {

    @Resource
    private ExportQueue queue;
    @Resource
    private ExportManager manager;

    @GetMapping("/queue")
    public Result<Object> queue() {
        return success(queue.getQueueByUid(AuthUtil.getUidAndCheck()));
    }

    @Lock(key = CacheKeyConstant.LOCK_DOWNLOAD, params = "#taskId", timeUnit = TimeUnit.MINUTES)
    @GetMapping("/download")
    public void download(@RequestParam String taskId, HttpServletResponse response) {
        manager.download(taskId, response);
    }

}
